package com.lianup.noname.deal;

import com.lianup.noname.common.HttpStatusEnum;
import com.lianup.noname.common.HttpVersionConstant;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.Collection;
import java.util.Locale;

public class HttpResponse implements HttpServletResponse{

    private OutputStream outputStream;

    private HttpRequest httpRequest;

    private PrintWriter writer;


    public HttpResponse(OutputStream outputStream, HttpRequest HttpRequest){

        this.outputStream = outputStream;
        this.httpRequest = HttpRequest;
    }



    public void finishResponse() {
        // sendHeaders();
        // Flush and close the appropriate output mechanism
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    }


    /**
     * 从uri中读取file
     */
    public void accessStaticResouce(){
        String uri = this.httpRequest.getRequestURI();
        try {
            // 此方法围在springboot下获取resouces资源的方法，且在linux环境下无效
//        File staticResouse = ResourceUtils.getFile("classpath:"+uri);
            URL url = HttpResponse.class.getClassLoader().getResource("static"+uri);
//            System.out.println(HttpResponse.class.getClassLoader().getResource("TestServlet"));
            if(url == null){
                outputStream.write(headToByte(HttpStatusEnum.NOT_FOUND));
                throw new FileNotFoundException();
            }
            File staticResouse = new File(url.getPath().substring(1));
            outputStream.write(headToByte(HttpStatusEnum.OK));
            write(staticResouse);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    /**
     * 返回状态时，把请求头转为byte[]数组
     * 如果没有返回请求头，则不会以网页，而是字符串的形式显示
     */
    public byte[] headToByte(HttpStatusEnum httpStatus){
        return new StringBuilder().append(HttpVersionConstant.HTTP_1_1).append(" ")
                .append(httpStatus.getStatus()).append(" ")
                .append(httpStatus.getDesc()).append("\r\n\r\n")
                .toString().getBytes();
    }

    /**
     * 把file中的内容写进outputStream
     */
    private void write(File staticResouce){
        try {
            FileInputStream fileInputStream = new FileInputStream(staticResouce);
            int count = 0;
            byte[] cache = new byte[1024];
            while ((count = fileInputStream.read(cache,0,1024)) != -1){
                outputStream.write(cache,0,count);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public String getCharacterEncoding() {
        return "ISO-8859-1";
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (writer != null) {
            return writer;
        }
        return (writer = new PrintWriter(outputStream));
    }

    @Override
    public void setCharacterEncoding(String charset) {

    }

    @Override
    public void setContentLength(int len) {

    }

    @Override
    public void setContentLengthLong(long l) {

    }

    @Override
    public void setContentType(String type) {

    }

    @Override
    public void setBufferSize(int size) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale loc) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    public HttpRequest getRequest() {
        return httpRequest;
    }

    @Override
    public void addCookie(Cookie cookie) {

    }

    @Override
    public boolean containsHeader(String name) {
        return false;
    }

    @Override
    public String encodeURL(String url) {
        return null;
    }

    @Override
    public String encodeRedirectURL(String url) {
        return null;
    }

    @Override
    public String encodeUrl(String url) {
        return null;
    }

    @Override
    public String encodeRedirectUrl(String url) {
        return null;
    }

    @Override
    public void sendError(int sc, String msg) throws IOException {

    }

    @Override
    public void sendError(int sc) throws IOException {

    }

    @Override
    public void sendRedirect(String location) throws IOException {

    }

    @Override
    public void setDateHeader(String name, long date) {

    }

    @Override
    public void addDateHeader(String name, long date) {

    }

    @Override
    public void setHeader(String name, String value) {

    }

    @Override
    public void addHeader(String name, String value) {

    }

    @Override
    public void setIntHeader(String name, int value) {

    }

    @Override
    public void addIntHeader(String name, int value) {

    }

    @Override
    public void setStatus(int sc) {

    }

    @Override
    public void setStatus(int sc, String sm) {

    }

    @Override
    public int getStatus() {
        return 0;
    }

    @Override
    public String getHeader(String name) {
        return null;
    }

    @Override
    public Collection<String> getHeaders(String name) {
        return null;
    }

    @Override
    public Collection<String> getHeaderNames() {
        return null;
    }


}
